home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / AC3 / AC3Dec / bit_allocate.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  17.4 KB  |  495 lines

  1. /* 
  2.  *  bit_allocate.c
  3.  *
  4.  *    Copyright (C) Aaron Holtzman - May 1999
  5.  *
  6.  *  This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7.  *    
  8.  *  ac3dec is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  ac3dec is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "ac3.h"
  27. #include "ac3_internal.h"
  28.  
  29.  
  30.  
  31. static inline sint_16 logadd(sint_16 a,sint_16  b);
  32. static sint_16 calc_lowcomp(sint_16 a,sint_16 b0,sint_16 b1,sint_16 bin);
  33. static inline uint_16 minAC3(sint_16 a,sint_16 b);
  34. static inline uint_16 maxAC3(sint_16 a,sint_16 b);
  35. static void ba_compute_psd(sint_16 start, sint_16 end, sint_16 exps[], 
  36.         sint_16 psd[], sint_16 bndpsd[]);
  37.  
  38. static void ba_compute_excitation(sint_16 start, sint_16 end,sint_16 fgain,
  39.         sint_16 fastleak, sint_16 slowleak, sint_16 is_lfe, sint_16 bndpsd[],
  40.         sint_16 excite[]);
  41. static void ba_compute_mask(sint_16 start, sint_16 end, uint_16 fscod,
  42.         uint_16 deltbae, uint_16 deltnseg, uint_16 deltoffst[], uint_16 deltba[],
  43.         uint_16 deltlen[], sint_16 excite[], sint_16 mask[]);
  44. static void ba_compute_bap(sint_16 start, sint_16 end, sint_16 snroffset,
  45.         sint_16 psd[], sint_16 mask[], sint_16 bap[]);
  46.  
  47. /* Misc LUTs for bit allocation process */
  48.  
  49. static sint_16 slowdec[]  = { 0x0f,  0x11,  0x13,  0x15  };
  50. static sint_16 fastdec[]  = { 0x3f,  0x53,  0x67,  0x7b  };
  51. static sint_16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
  52. static sint_16 dbpbtab[]  = { 0x000, 0x700, 0x900, 0xb00 };
  53.  
  54. static uint_16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
  55. static sint_16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400  };
  56.  
  57.  
  58. static sint_16 bndtab[] = {  0,  1,  2,   3,   4,   5,   6,   7,   8,   9, 
  59.                      10, 11, 12,  13,  14,  15,  16,  17,  18,  19,
  60.                      20, 21, 22,  23,  24,  25,  26,  27,  28,  31,
  61.                      34, 37, 40,  43,  46,  49,  55,  61,  67,  73,
  62.                      79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
  63.  
  64. static sint_16 bndsz[]  = { 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
  65.                      1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
  66.                      1,  1,  1,  1,  1,  1,  1,  1,  3,  3,
  67.                      3,  3,  3,  3,  3,  6,  6,  6,  6,  6,
  68.                      6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
  69.  
  70. static sint_16 masktab[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
  71.                      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29,
  72.                      29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34,
  73.                      34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37,
  74.                      37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40,
  75.                      40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
  76.                      41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43,
  77.                      43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44,
  78.                      44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  79.                      45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46,
  80.                      46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
  81.                      46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
  82.                      47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48,
  83.                      48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
  84.                      48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
  85.                      49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,  0,  0,  0 };
  86.  
  87.  
  88. static sint_16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
  89.                     0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032,
  90.                     0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
  91.                     0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026,
  92.                     0x0025, 0x0024, 0x0024, 0x0023, 0x0023, 0x0022, 0x0021, 0x0021,
  93.                     0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c,
  94.                     0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018,
  95.                     0x0018, 0x0017, 0x0017, 0x0016, 0x0016, 0x0015, 0x0015, 0x0015,
  96.                     0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012,
  97.                     0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f,
  98.                     0x000f, 0x000e, 0x000e, 0x000e, 0x000d, 0x000d, 0x000d, 0x000d,
  99.                     0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b,
  100.                     0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009,
  101.                     0x0009, 0x0009, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
  102.                     0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006,
  103.                     0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005,
  104.                     0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004,
  105.                     0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
  106.                     0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
  107.                     0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0002,
  108.                     0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
  109.                     0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
  110.                     0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  111.                     0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  112.                     0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  113.                     0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  114.                     0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  115.                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  116.                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  117.                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  118.                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  119.                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  120.                     0x0000, 0x0000, 0x0000, 0x0000};
  121.  
  122. static sint_16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,  
  123.                       0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390,  
  124.                       0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,  
  125.                       0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0,
  126.                       0x02f0, 0x02f0, 0x0300, 0x0310, 0x0340, 0x0390, 0x03e0, 0x0420,
  127.                       0x0460, 0x0490, 0x04a0, 0x0460, 0x0440, 0x0440, 0x0520, 0x0800,
  128.                       0x0840, 0x0840 },
  129.                       
  130.                     { 0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 
  131.                       0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 
  132.                       0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 
  133.                       0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0, 
  134.                       0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0320, 0x0350, 0x0390, 0x03e0, 
  135.                       0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630, 
  136.                       0x0840, 0x0840 },
  137.                       
  138.                     { 0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0, 
  139.                       0x03c0, 0x03b0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 
  140.                       0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390, 
  141.                       0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330, 
  142.                       0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310, 
  143.                       0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440, 
  144.                       0x0450, 0x04e0 }};
  145.  
  146.  
  147. static sint_16 baptab[] = { 0,  1,  1,  1,  1,  1,  2,  2,  3,  3,  3,  4,  4,  5,  5,  6,
  148.                      6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10, 
  149.                      10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
  150.                      14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
  151.  
  152. static sint_16 sdecay;
  153. static sint_16 fdecay;
  154. static sint_16 sgain;
  155. static sint_16 dbknee;
  156. static sint_16 floor;
  157. static sint_16 psd[256];
  158. static sint_16 bndpsd[256];
  159. static sint_16 excite[256];
  160. static sint_16 mask[256];
  161.  
  162. static inline uint_16
  163. maxAC3(sint_16 a,sint_16 b)
  164. {
  165.     return (a > b ? a : b);
  166. }
  167.     
  168. static inline uint_16
  169. minAC3(sint_16 a,sint_16 b)
  170. {
  171.     return (a < b ? a : b);
  172. }
  173.  
  174. static inline sint_16 
  175. logadd(sint_16 a,sint_16  b) 
  176.     sint_16 c;
  177.     sint_16 address;
  178.  
  179.     c = a - b; 
  180.     address = minAC3((abs(c) >> 1), 255); 
  181.     
  182.     if (c >= 0) 
  183.         return(a + latab[address]); 
  184.     else 
  185.         return(b + latab[address]); 
  186. }
  187.  
  188.  
  189. void bit_allocate(uint_16 fscod, bsi_t *bsi, audblk_t *audblk)
  190. {
  191.     uint_16 i;
  192.     sint_16 fgain;
  193.     sint_16 snroffset;
  194.     sint_16 start;
  195.     sint_16 end;
  196.     sint_16 fastleak;
  197.     sint_16 slowleak;
  198.  
  199.     /* Only perform bit_allocation if the exponents have changed or we
  200.      * have new sideband information */
  201.     if (audblk->chexpstr[0]  == 0 && audblk->chexpstr[1] == 0 &&
  202.             audblk->chexpstr[2]  == 0 && audblk->chexpstr[3] == 0 &&
  203.             audblk->chexpstr[4]  == 0 && audblk->cplexpstr   == 0 &&
  204.             audblk->lfeexpstr    == 0 && audblk->baie        == 0 &&
  205.             audblk->snroffste    == 0 && audblk->deltbaie    == 0)
  206.         return;
  207.  
  208.     /* Do some setup before we do the bit alloc */
  209.     sdecay = slowdec[audblk->sdcycod]; 
  210.     fdecay = fastdec[audblk->fdcycod];
  211.     sgain = slowgain[audblk->sgaincod]; 
  212.     dbknee = dbpbtab[audblk->dbpbcod]; 
  213.     floor = floortab[audblk->floorcod]; 
  214.  
  215.     /* if all the SNR offset constants are zero then the whole block is zero */
  216.     if(!audblk->csnroffst    && !audblk->fsnroffst[0] && 
  217.          !audblk->fsnroffst[1] && !audblk->fsnroffst[2] && 
  218.          !audblk->fsnroffst[3] && !audblk->fsnroffst[4] &&
  219.          !audblk->cplfsnroffst && !audblk->lfefsnroffst)
  220.     {
  221.         memset(audblk->fbw_bap,0,sizeof(uint_16) * 256 * 5);
  222.         memset(audblk->cpl_bap,0,sizeof(uint_16) * 256);
  223.         memset(audblk->lfe_bap,0,sizeof(uint_16) * 7);
  224.         return;
  225.     }
  226.          
  227.  
  228.     for(i = 0; i < bsi->nfchans; i++)
  229.     {
  230.         start = 0;
  231.         end = audblk->endmant[i] ; 
  232.         fgain = fastgain[audblk->fgaincod[i]]; 
  233.         snroffset = (((audblk->csnroffst - 15) << 4) + audblk->fsnroffst[i]) << 2 ;
  234.         fastleak = 0;
  235.         slowleak = 0;
  236.  
  237.         ba_compute_psd(start, end, audblk->fbw_exp[i], psd, bndpsd);
  238.  
  239.         ba_compute_excitation(start, end , fgain, fastleak, slowleak, 0, bndpsd, excite);
  240.  
  241.         ba_compute_mask(start, end, fscod, audblk->deltbae[i], audblk->deltnseg[i], 
  242.                 audblk->deltoffst[i], audblk->deltba[i], audblk->deltlen[i], excite, mask);
  243.  
  244.         ba_compute_bap(start, end, snroffset, psd, mask, audblk->fbw_bap[i]);
  245.     }
  246.  
  247.     if(audblk->cplinu)
  248.     {
  249.         start = audblk->cplstrtmant; 
  250.         end = audblk->cplendmant; 
  251.         fgain = fastgain[audblk->cplfgaincod];
  252.         snroffset = (((audblk->csnroffst - 15) << 4) + audblk->cplfsnroffst) << 2 ;
  253.         fastleak = (audblk->cplfleak << 8) + 768; 
  254.         slowleak = (audblk->cplsleak << 8) + 768;
  255.  
  256.         ba_compute_psd(start, end, audblk->cpl_exp, psd, bndpsd);
  257.  
  258.         ba_compute_excitation(start, end , fgain, fastleak, slowleak, 0, bndpsd, excite);
  259.  
  260.         ba_compute_mask(start, end, fscod, audblk->cpldeltbae, audblk->cpldeltnseg, 
  261.                 audblk->cpldeltoffst, audblk->cpldeltba, audblk->cpldeltlen, excite, mask);
  262.  
  263.         ba_compute_bap(start, end, snroffset, psd, mask, audblk->cpl_bap);
  264.     }
  265.  
  266.     if(bsi->lfeon)
  267.     {
  268.         start = 0;
  269.         end = 7;
  270.         fgain = fastgain[audblk->lfefgaincod];
  271.         snroffset = (((audblk->csnroffst - 15) << 4) + audblk->lfefsnroffst) << 2 ;
  272.         fastleak = 0;
  273.         slowleak = 0;
  274.  
  275.         ba_compute_psd(start, end, audblk->lfe_exp, psd, bndpsd);
  276.  
  277.         ba_compute_excitation(start, end , fgain, fastleak, slowleak, 1, bndpsd, excite);
  278.  
  279.         /* Perform no delta bit allocation for lfe */
  280.         ba_compute_mask(start, end, fscod, 2, 0, 0, 0, 0, excite, mask);
  281.  
  282.         ba_compute_bap(start, end, snroffset, psd, mask, audblk->lfe_bap);
  283.     }
  284. }
  285.  
  286.  
  287. static void ba_compute_psd(sint_16 start, sint_16 end, sint_16 exps[], 
  288.         sint_16 psd[], sint_16 bndpsd[])
  289. {
  290.     int bin,i,j,k;
  291.     sint_16 lastbin = 0;
  292.     
  293.     /* Map the exponents into dBs */
  294.     for (bin=start; bin<end; bin++) 
  295.     { 
  296.         psd[bin] = (3072 - (exps[bin] << 7)); 
  297.     }
  298.  
  299.     /* Integrate the psd function over each bit allocation band */
  300.     j = start; 
  301.     k = masktab[start]; 
  302.     
  303.     do 
  304.     { 
  305.         lastbin = minAC3(bndtab[k] + bndsz[k], end); 
  306.         bndpsd[k] = psd[j]; 
  307.         j++; 
  308.  
  309.         for (i = j; i < lastbin; i++) 
  310.         { 
  311.             bndpsd[k] = logadd(bndpsd[k], psd[j]);
  312.             j++; 
  313.         } 
  314.         
  315.         k++; 
  316.     } while (end > lastbin);
  317. }
  318.  
  319. static void ba_compute_excitation(sint_16 start, sint_16 end,sint_16 fgain,
  320.         sint_16 fastleak, sint_16 slowleak, sint_16 is_lfe, sint_16 bndpsd[],
  321.         sint_16 excite[])
  322. {
  323.     int bin;
  324.     sint_16 bndstrt;
  325.     sint_16 bndend;
  326.     sint_16 lowcomp = 0;
  327.     sint_16 begin = 0;
  328.  
  329.     /* Compute excitation function */
  330.     bndstrt = masktab[start]; 
  331.     bndend = masktab[end - 1] + 1; 
  332.     
  333.     if (bndstrt == 0) /* For fbw and lfe channels */ 
  334.     { 
  335.         lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0); 
  336.         excite[0] = bndpsd[0] - fgain - lowcomp; 
  337.         lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
  338.         excite[1] = bndpsd[1] - fgain - lowcomp; 
  339.         begin = 7 ; 
  340.         
  341.         /* Note: Do not call calc_lowcomp() for the last band of the lfe channel, (bin = 6) */ 
  342.         for (bin = 2; bin < 7; bin++) 
  343.         { 
  344.             if (!(is_lfe && (bin == 6)))
  345.                 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin); 
  346.             fastleak = bndpsd[bin] - fgain; 
  347.             slowleak = bndpsd[bin] - sgain; 
  348.             excite[bin] = fastleak - lowcomp; 
  349.             
  350.             if (!(is_lfe && (bin == 6)))
  351.             {
  352.                 if (bndpsd[bin] <= bndpsd[bin+1]) 
  353.                 {
  354.                     begin = bin + 1 ; 
  355.                     break; 
  356.                 } 
  357.             }
  358.         } 
  359.         
  360.         for (bin = begin; bin < minAC3(bndend, 22); bin++) 
  361.         { 
  362.             if (!(is_lfe && (bin == 6)))
  363.                 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin); 
  364.             fastleak -= fdecay ; 
  365.             fastleak = maxAC3(fastleak, bndpsd[bin] - fgain); 
  366.             slowleak -= sdecay ; 
  367.             slowleak = maxAC3(slowleak, bndpsd[bin] - sgain); 
  368.             excite[bin] = maxAC3(fastleak - lowcomp, slowleak); 
  369.         } 
  370.         begin = 22; 
  371.     } 
  372.     else /* For coupling channel */ 
  373.     { 
  374.         begin = bndstrt; 
  375.     } 
  376.  
  377.     for (bin = begin; bin < bndend; bin++) 
  378.     { 
  379.         fastleak -= fdecay; 
  380.         fastleak = maxAC3(fastleak, bndpsd[bin] - fgain); 
  381.         slowleak -= sdecay; 
  382.         slowleak = maxAC3(slowleak, bndpsd[bin] - sgain); 
  383.         excite[bin] = maxAC3(fastleak, slowleak) ; 
  384.     } 
  385. }
  386.  
  387. static void ba_compute_mask(sint_16 start, sint_16 end, uint_16 fscod,
  388.         uint_16 deltbae, uint_16 deltnseg, uint_16 deltoffst[], uint_16 deltba[],
  389.         uint_16 deltlen[], sint_16 excite[], sint_16 mask[])
  390. {
  391.     int bin,k;
  392.     sint_16 bndstrt;
  393.     sint_16 bndend;
  394.     sint_16 delta;
  395.  
  396.     bndstrt = masktab[start]; 
  397.     bndend = masktab[end - 1] + 1; 
  398.  
  399.     /* Compute the masking curve */
  400.  
  401.     for (bin = bndstrt; bin < bndend; bin++) 
  402.     { 
  403.         if (bndpsd[bin] < dbknee) 
  404.         { 
  405.             excite[bin] += ((dbknee - bndpsd[bin]) >> 2); 
  406.         } 
  407.         mask[bin] = maxAC3(excite[bin], hth[fscod][bin]);
  408.     }
  409.     
  410.     /* Perform delta bit modulation if necessary */
  411.     if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW)) 
  412.     { 
  413.         sint_16 band = 0; 
  414.         sint_16 seg = 0; 
  415.         
  416.         for (seg = 0; seg < deltnseg+1; seg++) 
  417.         { 
  418.             band += deltoffst[seg]; 
  419.             if (deltba[seg] >= 4) 
  420.             { 
  421.                 delta = (deltba[seg] - 3) << 7;
  422.             } 
  423.             else 
  424.             { 
  425.                 delta = (deltba[seg] - 4) << 7;
  426.             } 
  427.             
  428.             for (k = 0; k < deltlen[seg]; k++) 
  429.             { 
  430.                 mask[band] += delta; 
  431.                 band++; 
  432.             } 
  433.         } 
  434.     }
  435. }
  436.  
  437. static void ba_compute_bap(sint_16 start, sint_16 end, sint_16 snroffset,
  438.         sint_16 psd[], sint_16 mask[], sint_16 bap[])
  439. {
  440.     int i,j,k;
  441.     sint_16 lastbin = 0;
  442.     sint_16 address = 0;
  443.  
  444.     /* Compute the bit allocation pointer for each bin */
  445.     i = start; 
  446.     j = masktab[start]; 
  447.  
  448.     do 
  449.     { 
  450.         lastbin = minAC3(bndtab[j] + bndsz[j], end); 
  451.         mask[j] -= snroffset; 
  452.         mask[j] -= floor; 
  453.         
  454.         if (mask[j] < 0) 
  455.             mask[j] = 0; 
  456.  
  457.         mask[j] &= 0x1fe0;
  458.         mask[j] += floor; 
  459.         for (k = i; k < lastbin; k++) 
  460.         { 
  461.             address = (psd[i] - mask[j]) >> 5; 
  462.             address = minAC3(63, maxAC3(0, address)); 
  463.             bap[i] = baptab[address]; 
  464.             i++; 
  465.         } 
  466.         j++; 
  467.     } while (end > lastbin);
  468. }
  469.  
  470. static sint_16 
  471. calc_lowcomp(sint_16 a,sint_16 b0,sint_16 b1,sint_16 bin) 
  472.  
  473.     if (bin < 7) 
  474.     { 
  475.         if ((b0 + 256) == b1)
  476.             a = 384; 
  477.          else if (b0 > b1) 
  478.             a = maxAC3(0, a - 64); 
  479.     } 
  480.     else if (bin < 20) 
  481.     { 
  482.         if ((b0 + 256) == b1) 
  483.             a = 320; 
  484.         else if (b0 > b1) 
  485.             a = maxAC3(0, a - 64) ; 
  486.     }
  487.     else  
  488.         a = maxAC3(0, a - 128); 
  489.     
  490.     return(a);
  491. }
  492.  
  493.